package com.my.financial.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.my.financial.controller.form.AddForm;
import com.my.financial.controller.form.PageInfo;
import com.my.financial.controller.form.QueryForm;
import com.my.financial.domain.FinancialAccount;
import com.my.financial.domain.FinancialItem;
import com.my.financial.domain.FinancialService;
import com.my.financial.domain.User;

@Controller
@SessionAttributes("currUser")
public class FinancialController {
	
	@Autowired
	private FinancialService financialService;

	@RequestMapping(value = "/addItem", method = RequestMethod.GET)
	public ModelAndView addItem(@ModelAttribute("currUser") User user, 
			String payType) {
		ModelAndView mv = new ModelAndView("addItem");
		if(StringUtils.isNotBlank(payType)&&payType.equals("income")){ 
			mv.addObject("payType", "收入");
		}else if(StringUtils.isNotBlank(payType)&&payType.equals("payout")){
			mv.addObject("payType", "支出");
		}
		List<String> memberList = financialService.findAllMembers(user.getId());
		List<FinancialAccount> accountList = financialService.findAllAccount(user.getId());
		mv.addObject("memberList", memberList);
		mv.addObject("accountList", accountList);
		return mv.addObject(new AddForm());
	}

	@RequestMapping(value = "/addItem", method = RequestMethod.POST)
	public ModelAndView doAddItem(@Valid AddForm addForm,BindingResult result,
			@ModelAttribute("currUser")User user) {
		ModelAndView mv = new ModelAndView("addItem");
		List<String> memberList = financialService.findAllMembers(user.getId());
		List<FinancialAccount> accountList = financialService.findAllAccount(user.getId());
		mv.addObject("accountList", accountList);
		mv.addObject("memberList", memberList);
		if (!result.hasErrors()) {
			addForm.setUser(user);
			FinancialItem item = formToItem(addForm);
			financialService.saveItem(item);
			mv.addObject("display", "inline");
			mv.addObject("message", "记账成功");
		}
		return mv;
	}

	@RequestMapping(value = "/memberManage", method = RequestMethod.GET)
	public ModelAndView memberManage(@ModelAttribute("currUser")User currUser) {
		ModelAndView mv = new ModelAndView("memberManage");
		List<String> memberList = financialService.findAllMembers(currUser.getId());
		mv.addObject("memberList", memberList);
		return mv;
	}
	
	@RequestMapping(value = "/createMember", method = RequestMethod.POST )
	public RedirectView createMember(@ModelAttribute("currUser")User user,String member) {
		RedirectView rv = new RedirectView("memberManage");
		if(StringUtils.isNotBlank(member)){
			financialService.addMember(member,user.getId());
		}
		return rv;
	}
	
	@RequestMapping(value = "/deleteMember")
	public RedirectView deleteMember(@ModelAttribute("currUser")User user,String member) {
		RedirectView rv = new RedirectView("memberManage");
		if(StringUtils.isNotBlank(member)){
			financialService.deleteMember(member,user.getId());
		}
		return rv;
	}
	
	@RequestMapping(value = "/query", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView query(QueryForm queryForm,String pageNum,BindingResult result,
		@ModelAttribute("currUser")User user) {
		//1.设置查询需要的参数
		queryForm.setUser(user);
		if (pageNum == null) {pageNum="1";}
		PageInfo pageInfo = new PageInfo(Integer.parseInt(pageNum));
		queryForm.setUser(user);
		//2.数据库查询需要的数据
		List<FinancialItem> viewList = financialService.findItemByQueryForm(queryForm,pageInfo);
		pageInfo = financialService.findPageInfo(queryForm,pageInfo);
		List<String> memberList = financialService.findAllMembers(user.getId());
		//3.返回数据参数视图层显示
		ModelAndView mv = new ModelAndView("center-table");
		mv.addObject("memberList", memberList);
		mv.addObject("viewList",viewList);
		mv.addObject("pageInfo",pageInfo);
		return mv;
	}
	
	@RequestMapping(value = "/queryByType")
	public ModelAndView queryByType(@RequestParam(required=false) String pageNum,
			String dateType,String dateNum,@ModelAttribute("currUser")User user) {
		//1.获取查询需要的参数
		if (pageNum == null) {pageNum="1";}
		if (dateNum == null) {dateNum="0";}
		QueryForm queryForm = financialService.getStartAndEndTime(dateType,dateNum);
		queryForm.setUser(user);
		PageInfo pageInfo = new PageInfo(Integer.parseInt(pageNum));
		//2.数据库查询需要的数据
		String dateRange = financialService.getDateRange(queryForm,dateType);
		List<FinancialItem> viewList = financialService.findItemByQueryForm(queryForm,pageInfo);
		pageInfo = financialService.findPageInfo(queryForm,pageInfo);
		//3.返回数据视图层显示
		ModelAndView mv = new ModelAndView("sortTable");
		mv.addObject("viewList",viewList);
		mv.addObject("pageInfo",pageInfo);
		mv.addObject("dateRange",dateRange);
		mv.addObject("dateType",dateType);
		mv.addObject("dateNum",Integer.parseInt(dateNum));
		return mv;
	}
	
	@RequestMapping(value = "/deleteItem", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void deleteItem(HttpServletRequest request,String multiId){
		financialService.batchDeleteItemById(multiId);
	}
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index() {
		return new ModelAndView("index").addObject(new QueryForm());
	}
	
	@RequestMapping(value = "/top", method = RequestMethod.GET)
	public ModelAndView top() {
		return new ModelAndView("top");
	}
	@RequestMapping(value = "/down", method = RequestMethod.GET)
	public ModelAndView down() {
		return new ModelAndView("down");
	}
	@RequestMapping(value = "/center", method = RequestMethod.GET)
	public ModelAndView center() {
		return new ModelAndView("center");
	}
	@RequestMapping(value = "/center-left", method = RequestMethod.GET)
	public ModelAndView centerLeft() {
		return new ModelAndView("center-left");
	}
	@RequestMapping(value = "/center-table", method = RequestMethod.GET)
	public ModelAndView centerTable(@ModelAttribute("currUser") User currUser,Model model) {
		ModelAndView mv = new ModelAndView("center-table");
		Long id = currUser.getId();
		List<String> memberList = financialService.findAllMembers(id);
		mv.addObject("memberList", memberList);
		return mv.addObject(new QueryForm());
	}
	
	private FinancialItem formToItem(AddForm addForm) {
		FinancialItem  financialItem  = new FinancialItem();
		Long accountId = Long.parseLong(addForm.getAccountId());
		financialItem.setItem(addForm.getItem());
		financialItem.setType(addForm.getType());
		financialItem.setSummary(addForm.getSummary());
		financialItem.setMember(addForm.getMember());
		financialItem.setAccount(new FinancialAccount(accountId));
		financialItem.setAmount(new BigDecimal(addForm.getAmount()));
		financialItem.setUser(addForm.getUser());
		return financialItem;
	}
	
}
